import copy
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from Notes.models import Note
from SecureHelperServer.settings import formatResponseData
from utils.conf import verifyID, Response
from utils.custom import MyJWTAuthentication


class RemoveNoteView(APIView):
    # 局部(本视图有效)认证类
    authentication_classes = [MyJWTAuthentication]
    # 局部(本视图有效)权限类
    permission_classes = [IsAuthenticated]

    def post(self, request, *args, **kwargs):
        """
        删除Note
        """
        data = copy.deepcopy(formatResponseData)
        try:
            removeNoteIdList = request.data.get("removeNoteIdList")

            if not removeNoteIdList:
                return Response(data=data, responseMsg="请求参数缺失", loggerMsg="请求参数缺失", isError=True)

            for noteId in removeNoteIdList:
                if not verifyID(noteId):
                    return Response(data=data, responseMsg="记录不存在",
                                    loggerMsg=f"删除Note：noteId={noteId}不是一个有效的id", isError=True)

                note = Note.objects.filter(id=noteId, user=request.user).first()

                if not note:
                    return Response(data=data, responseMsg="记录不存在",
                                    loggerMsg=f"删除Note：未找到noteId={noteId}对应的note", isError=True)
                # 删除note
                note.delete()

            return Response(data=data, responseMsg="删除成功",
                            loggerMsg=f"删除Note: 成功，uid={request.user.uid}，removeNoteIdList={removeNoteIdList}")

        except Exception as e:
            return Response(data=data, responseMsg="ServerError",
                            loggerMsg=f"请求处理失败: {e}", isError=True)
